package com.shopping.controller;

import com.shopping.dto.UserDTO;
import com.shopping.dto.UserQueryDTO;
import com.shopping.dto.UserUpdateDTO;
import com.shopping.dto.PasswordResetDTO;
import com.shopping.service.UserService;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/admin/users")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminUserController {
    private final UserService userService;

    @GetMapping
    public ResponseEntity<Page<UserDTO>> getUserList(UserQueryDTO queryDTO) {
        return ResponseEntity.ok(userService.getUserList(queryDTO));
    }

    @GetMapping("/{id}")
    public ResponseEntity<UserDTO> getUserById(@PathVariable Long id) {
        return ResponseEntity.ok(userService.getUserById(id));
    }

    @PutMapping("/{id}")
    public ResponseEntity<UserDTO> updateUser(
            @PathVariable Long id,
            @Valid @RequestBody UserUpdateDTO updateDTO
    ) {
        return ResponseEntity.ok(userService.updateUser(id, updateDTO));
    }

    @PutMapping("/{id}/password")
    public ResponseEntity<Void> resetPassword(
            @PathVariable Long id,
            @Valid @RequestBody PasswordResetDTO resetDTO
    ) {
        userService.resetPassword(id, resetDTO);
        return ResponseEntity.ok().build();
    }

    @PutMapping("/{id}/status")
    public ResponseEntity<Void> toggleUserStatus(@PathVariable Long id) {
        userService.toggleUserStatus(id);
        return ResponseEntity.ok().build();
    }
}